
#include<stdio.h>
#include<stdlib.h>

unsigned int CRCTable[16] =
{
    0x0000, 0xcc01, 0xd801, 0x1400, 0xf001, 0x3c00, 0x2800, 0xe401,
    0xA001, 0x6c00, 0x7800, 0xb401, 0x5000, 0x9c01, 0x8801, 0x4400
};

unsigned int test_crc(unsigned char *pchMsg, unsigned int DataLen)
{
    unsigned char i;
    unsigned char chChar;
    unsigned int icrc = 0xffff;

    for(i = 0; i < DataLen; i++)
    {
        chChar = *pchMsg++;
        icrc = CRCTable[(chChar ^ icrc) & 15] ^ (icrc >> 4);
        icrc = CRCTable[((chChar >> 4) ^ icrc) & 15] ^ (icrc >> 4);
    }
    
    printf("\n");
    printf("icrc = %x\n", icrc);

    return icrc;
}

void main()
{
    /* 模拟植物生长 */
    unsigned char test_data[] = {0x02, 0x03, 0x09, 0x00, 0x11, 0x00, 0x11, 0x00, 0x11, 0x00, 0x11, 0x11, 0x48, 0xef};

    test_crc(test_data, (unsigned int)sizeof(test_data)-2);

    return;
}